package com.bc004346.travelus;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

import com.bc004346.travelus.Model.Abstract.FlightListActivity;
import com.bc004346.travelus.Model.Domain.Flight;
import com.bc004346.travelus.Utility.Utility;

import java.util.List;

import static com.bc004346.travelus.Utility.Utility.log;

/**
 * Created by Renats on 9/26/2014.
 * PBT-specific implementation
 */
public class PBTFlightListActivity extends FlightListActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTopProgressBar = (ProgressBar) findViewById(R.id.activityFragmentProgressBar);
        mTopProgressBar.setVisibility(View.VISIBLE);
        setTitle("PBT Tracker");
        refresh();
    }

    @Override
    protected void refresh() {

        mStatus.setText("Getting PBT");
        new AsyncTask<Void, Void, List<Flight>>() {

            private Exception ex = null;

            @Override
            protected void onPostExecute(List<Flight> f) {

                if (ex != null) {
                    mStatus.setText(ex.getMessage());
                    log(ex, PBTFlightListActivity.this);
                } else if (f == null || f.size() == 0) {
                    mStatus.setText(getString(R.string.common_no_flights_found));
                } else {
                    mFragment.refreshFlights(f);
                    mStatus.setText("");
                }
            }

            @Override
            protected List<Flight> doInBackground(Void... params) {

                List<Flight> flights = null;
                try {
                    flights = Utility.getInstance().getTravelUS().pbtTracker();
                } catch (Exception e) {
                    ex = e;
                }
                return flights;
            }
        }.execute();
        mTopProgressBar.setVisibility(View.INVISIBLE);
    }

    @Override
    protected String getTaskTag() {
        return "pbtFlightListActivity";
    }
}
